import queue
import time

import cv2

frame_queue = queue.Queue()


def Video():
    # 调用相机拍图的函数
    vid = cv2.VideoCapture(0)
    i = 1
    vid.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
    vid.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
    vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
    vid.set(cv2.CAP_PROP_FPS, 30)
    if not vid.isOpened():
        raise IOError("Couldn't open webcam or video")
    while (vid.isOpened()):
        return_value, frame = vid.read()
        # 原始图片推入队列中
        # print(frame.tostring())
        frame_queue.put(frame)
        # return
        # 负责移除队列中的旧图
        frame_queue.get() if frame_queue.qsize() > 1 else time.sleep(0.01)


if __name__ == '__main__':
    Video()
